package upeu.edu.pe.utils;


import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.security.Principal;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;



public class PresentTag extends TagSupport{

    public static final String ROLE_DELIMITER = ",";
    private static Map scopes;
    
    protected String cookie;
    protected String header;
    protected String name;
    protected String parameter;
    protected String property;
    protected String role;
    protected String scope;
    protected String user;
    
    @Override
    public int doStartTag()
        throws JspException
    {
        return !condition() ? 0 : 1;
    }

    @Override
    public int doEndTag()
        throws JspException
    {
        return 6;
    }
    
    protected boolean condition()
        throws JspException
    {
        return condition(true);
    }

    protected boolean condition(boolean desired)
        throws JspException
    {
        boolean present = false;
        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
        if(cookie != null){        	
            Cookie cookies[] = request.getCookies();
            if(cookies != null){
                for(int i = 0; i < cookies.length; i++){                	
                    if(!cookie.equals(cookies[i].getName()))
                        continue;
                    present = true;
                    break;
                }
            }
        }else if(header != null){        	
            String value = request.getHeader(header);
            present = value != null;
        }else if(name != null){        	
            Object value = null;
            try{
                if(property != null){
                    //value = lookup(pageContext, name, property, scope);
                }else if(role != null){                	
                	value=lookupr(pageContext, name, role);                	
                }else{
                	value = lookup(pageContext, name, scope);                	
                }
            }catch(JspException e){
                value = null;
            }
            present = value != null;
        }else if(parameter != null){
            	String value = request.getParameter(parameter);
            	present = value != null;
        }else if(user != null) {
            Principal principal = request.getUserPrincipal();            
            present = principal != null && user.equals(principal.getName());
        } else {
            JspException e = new JspException("milogic.selector");
//            RequestUtils.saveException(pageContext, e);
            throw e;
        }
        //System.out.println("afuera"+name);
        //System.out.println(present == desired);        
        return present == desired;
    }

    public static int getScope(String scopeName)
        throws JspException
    {
        Integer scope = (Integer)scopes.get(scopeName.toLowerCase());
        if(scope == null)
            throw new JspException("lookup.scope");
        else
            return scope.intValue();
    }
    

    public static Object lookup(PageContext pageContext, String name, String scopeName)throws JspException{
        if(scopeName == null){
        	return pageContext.findAttribute(name);
        }            
        return pageContext.getAttribute(name, getScope(scopeName));
//        JspException e;
//        e;
       // saveException(pageContext, e);
//        throw e;
    }

	public static Object lookupr(PageContext pageContext, String name, String role)throws JspException{		
		if(pageContext.findAttribute(name) != null){		
	        if(role != null){
	        	ArrayList roles = (ArrayList)pageContext.findAttribute("ListAccesses");
	        	for(int i=0; i<roles.size(); i++){	        		
					if(role.equals(roles.get(i))){						
						return roles.get(i);					
					}
				}        	        	
	        }
	     }	            
        return null;        
    }
	
	public void setCookie(String cookie) { this.cookie = cookie; }
	public void setHeader(String header) { this.header = header; }
	public void setName(String name) { this.name = name; }
	public void setParameter(String parameter) { this.parameter = parameter; }
	public void setProperty(String property) { this.property = property; }
	public void setRole(String role) { this.role = role; }
	public void setScope(String scope) { this.scope = scope; }
	public void setUser(String user) { this.user = user; }

	public static String getROLE_DELIMITER() { return (PresentTag.ROLE_DELIMITER); }
	public String getCookie() { return (this.cookie); }
	public String getHeader() { return (this.header); }
	public String getName() { return (this.name); }
	public String getParameter() { return (this.parameter); }
	public String getProperty() { return (this.property); }
	public String getRole() { return (this.role); }
	public String getScope() { return (this.scope); }
	public String getUser() { return (this.user); }


}
